package com.chy.blog_web.core.util;

import java.math.BigDecimal;
import java.security.MessageDigest;
import java.sql.Timestamp;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.alibaba.fastjson.JSONArray;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
import org.apache.commons.lang3.StringUtils;
import sun.misc.BASE64Encoder;

/**
 * 字符串处理工具类
 */
public class StringUtil {

    /**
     * 字符串数组转换为字符串
     *
     * @param a
     * @param sep
     * @return
     */
    public static String arrayToString(String[] a, String sep) {
        if (a == null || a.length == 0)
            return "";
        if (a.length == 1)
            return a[0];
        String b = "";
        for (int i = 0; i < a.length; i++) {
            b += a[i] + sep;
        }
        return b.substring(0, b.length() - sep.length());
    }

    /**
     * 字符串重复 将某字符串复制多遍，并用分隔符组成新的字符串
     *
     * @param sign      要复制的字符串单元
     * @param separator 分隔符
     * @param num       复制数量
     * @return
     */
    public static String genString(String sign, String separator, int num) {
        String r = "";
        for (int i = 0; i < num; i++) {
            r = r + sign + separator;
        }
        if (r.endsWith(separator)) {
            r = r.substring(0, r.length() - 1);
        }
        return r;
    }

    /**
     * 去掉字符串首尾的空格
     *
     * @param input
     * @return
     */
    public static String removeSpace(String input) {
        return removeBeginSpace(removeEndSpace(input));
    }

    public static String removeBeginSpace(String input) {
        if (input == null)
            return "";
        if (input.startsWith(" ")) {
            input = input.substring(1, input.length());
            return removeBeginSpace(input);
        }
        return input;
    }

    public static String removeEndSpace(String input) {
        if (input == null)
            return "";
        if (input.endsWith(" ")) {
            input = input.substring(0, input.length() - 1);
            return removeEndSpace(input);
        }
        return input;
    }

    /**
     * SQL注入关键字屏蔽
     */
    public static boolean sqlKeyWords(String str) {
        boolean flag = false;
        // SQL中的关键字，可以自己加
        String skws = "'|and|exec|insert|select|delete|update|count|*|%|chr|mid|master|truncate|char|declare|;|or|-|+|,|&|$|<|>|(|)";
        String[] skwArray = skws.split("\\|");
        String s = str.toLowerCase();
        for (int i = 0, len = skwArray.length; i < len; i++) {
            if (s.indexOf(skwArray[i]) > -1) {
                flag = true;
                break;
            }
        }
        return flag;
    }

    /**
     * 将List转为String[]
     *
     * @param list
     * @return
     */
    public static String[] listToArray(List<String> list) {
        if (list == null)
            return null;
        String[] a = new String[list.size()];
        for (int i = 0; i < list.size(); i++) {
            a[i] = list.get(i);
        }
        return a;
    }

    /**
     * 判断不为NULL
     *
     * @param a
     * @return
     */
    public static boolean notNull(String a) {
        return !isNull(a);
    }

    /**
     * 判断是否为空
     *
     * @param a
     * @return
     */
    public static boolean isNull(String a) {
        return a == null || "".equals(a.trim()) || "null".equals(a.trim());
    }

    public static String repeat(String item, int time) {
        StringBuffer b = new StringBuffer();
        for (int i = 0; i < time; i++) {
            b.append(item);
        }
        return b.toString();
    }

    public static int toInt(String a) {
        if (isNull(a))
            return 0;
        try {
            return new Integer(a).intValue();
        } catch (Exception e) {
        }
        return 0;
    }

    public static long toLong(String a) {
        if (isNull(a))
            return 0;
        try {
            return new Long(a).longValue();
        } catch (Exception e) {
        }
        return 0;
    }

    public static double toDouble(String a) {
        if (isNull(a))
            return 0;
        try {
            return new Double(a).doubleValue();
        } catch (Exception e) {
        }
        return 0;
    }

    public static float toFloat(String a) {
        if (isNull(a))
            return 0;
        try {
            return new Float(a).floatValue();
        } catch (Exception e) {
        }
        return 0;
    }

    public static String timeToString(Timestamp t) {
        if (t == null)
            return "";
        return t.toString();
    }

    public static String clobToString(java.sql.Clob clob) {
        try {
            return clob.getSubString((long) 1, (int) clob.length());
        } catch (Exception e) {
            // e.printStackTrace();
            return "";
        }
    }

    public static String objectToString(Object obj) {
        if (obj == null)
            return "";
        return obj.toString();
    }

    public static boolean istoInt(String value) {
        if (value == null || value.equals(""))
            return false;
        char[] c = value.toCharArray();
        boolean blReturn = true;
        for (int ni = 0; ni < c.length; ni++) {
            if (c[ni] < 48 || c[ni] > 57) //
            {
                blReturn = false;
                break;
            }
        }
        return blReturn;
    }

    public static boolean noInt(String value) {
        if (isNull(value))
            return false;
        char[] c = value.toCharArray();
        boolean blReturn = false;
        for (int ni = 0; ni < c.length; ni++) {
            if (c[ni] < 48 || c[ni] > 57) //
            {
                blReturn = true;
                break;
            }
        }
        return blReturn;
    }

    public static boolean checkValidDate(String pDateObj) {
        boolean ret = false;
        if (pDateObj == null || pDateObj.length() != 10) {
            ret = true;
        }
        try {
            int year = new Integer(pDateObj.substring(0, 4)).intValue();
            int month = new Integer(pDateObj.substring(6, 7)).intValue();
            int day = new Integer(pDateObj.substring(9)).intValue();
            Calendar cal = Calendar.getInstance();
            cal.setLenient(true);
            cal.set(year, month - 1, day);
            cal.getTime();
        } catch (Exception e) {
            ret = true;
        }
        return ret;
    }

    public static String firstCharUpperCase(String str) {
        str = str.toUpperCase().charAt(0) + str.substring(1);
        return str;
    }

    public static String changeStringCharSet(String str) {
        try {
            return new String(str.getBytes("ISO-8859-1"), "GBK");
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    /**
     * 将字符串转化成新的编码格式
     *
     * @param str
     * @param newCharset
     * @return
     */
    public static String changeCharset(String str, String newCharset) {
        try {
            if (str == null)
                return str;
            return new String(str.getBytes(), newCharset);
        } catch (Exception e) {
            return str;
        }
    }

    public static String[] changeStringCharSet(String[] str) {
        for (int i = 0; i < str.length; i++)
            str[i] = changeStringCharSet(str[i]);
        return str;
    }

    public static double doubleToDouble(double num, int i) {
        BigDecimal bigDecimal = new BigDecimal(num);
        return bigDecimal.setScale(i, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    public static boolean isEmpty(String str) {
        return (str == null) || "null".equals(str) || "".equals(str);
    }

    public static float floatToFloat(float num, int i) {
        BigDecimal bigDecimal = new BigDecimal(num);
        return bigDecimal.setScale(i, BigDecimal.ROUND_HALF_UP).floatValue();
    }

    public static String floatToString(float num) {
        String result = "0";
        try {
            result = Float.toString(num);
        } catch (Exception e) {
        }
        return result;
    }

    public static String[] stringToArray(String str) {
        return stringToArray(str, ",");
    }

    public static String[] stringToArray(String str, String splitString) {
        String[] staObj = str.split(splitString);
        List<String> list = new ArrayList<String>();
        for (int i = 0; i < staObj.length; i++) {
            if (!list.contains(staObj[i])) {
                list.add(staObj[i]);
            }
        }
        return list.toArray(new String[list.size()]);
    }

    // 求两个数组的交集
    public static String[] intersect(String[] arr1, String[] arr2) {
        Map<String, Boolean> map = new HashMap<String, Boolean>();
        LinkedList<String> list = new LinkedList<String>();
        for (String str : arr1) {
            if (!map.containsKey(str)) {
                map.put(str, Boolean.FALSE);
            }
        }
        for (String str : arr2) {
            if (map.containsKey(str)) {
                map.put(str, Boolean.TRUE);
            }
        }
        for (Entry<String, Boolean> e : map.entrySet()) {
            if (e.getValue().equals(Boolean.TRUE)) {
                list.add(e.getKey());
            }
        }
        String[] result = {};
        return list.toArray(result);
    }

    public static boolean isEqual(String a, String b) {
        if (a == null || b == null)
            return false;
        return a.equals(b);
    }

    public static String getNextNum(int length, String maxNum) {
        int next = toInt(maxNum) + 1;
        String nextString = next + "";
        int p = length - nextString.length();
        if (p > 0) {
            for (int i = 0; i < p; i++) {
                nextString = "0" + nextString;
            }
        }
        return nextString;
    }

    /**
     * 根据分隔符识别某字符串的前缀
     *
     * @param source 字符串
     * @param sep    分隔符
     * @return
     */
    public static String getPrefix(String source, String sep) {
        if (isNull(source) || isNull(sep))
            return null;
        int index = source.indexOf(sep);
        if (index > 0) {
            return source.split(sep)[0];
        }
        return null;
    }

    /**
     * 判断一个字符串是否为数字
     *
     * @param str
     * @return
     */
    public static boolean isNum(String str) {
        return str.matches("^[-+]?(([0-9]+)([.]([0-9]+))?|([.]([0-9]+))?)$");
    }

    /**
     * 保留两位小数
     */
    public static double mathDouble(double num) {
        BigDecimal b = new BigDecimal(num);
        return b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    /**
     * 数字加合
     *
     * @param a
     * @param b
     * @return
     */
    public static double doubleAdd(String a, String b) {
        return toDouble(a) + toDouble(b);
    }

    /**
     * 字符加密
     *
     * @param value
     * @return
     */
    public static String encrypt(String value) {
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            BASE64Encoder base64en = new BASE64Encoder();
            return base64en.encode(md5.digest(value.getBytes("utf-8")));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return value;
    }

    /**
     * 整数转换为字符串规定位数（位数不足补0）
     *
     * @param value 要转换的值
     * @param digit 位数
     * @return
     */
    public static String getIntDigits(int value, int digit) {
        NumberFormat numFormatter = NumberFormat.getNumberInstance();
        numFormatter.setMinimumIntegerDigits(digit);
        numFormatter.setGroupingUsed(false);
        return numFormatter.format(value);
    }

    /**
     * 替换字符串里括号的值
     *
     * @param value    原value : aa
     * @param newValue 要追加的新value : bb_
     * @return bb_aa
     */
    public static String replaceBrackets(String value, String newValue) {
        String[] patterns = {"\\([^)]+\\(", "\\)[^(]+\\)", "\\([^()]+\\)"};
        for (int i = 0; i < patterns.length; i++) {
            Matcher matcher = Pattern.compile(patterns[i]).matcher(value);
            while (matcher.find()) {
                String aa = matcher.group().substring(1, matcher.group().length() - 1);
                String newa = newValue + aa;
                value = value.replace(matcher.group(), matcher.group().replaceAll(matcher.group(), newa));
            }
        }
        return value;
    }

    public static String getColorString(String color, String name) {
        String str = "<font color=" + color + ">";
        str += name + "</font>";
        return str;
    }

    public static final boolean isChinese(char c) {
        Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
        if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
                || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
                || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
                || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
                || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
                || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {
            return true;
        }
        return false;
    }

    public static final boolean isChinese(String strName) {
        char[] ch = strName.toCharArray();
        for (int i = 0; i < ch.length; i++) {
            char c = ch[i];
            if (isChinese(c)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 处理的最大数字达千万亿位 精确到分
     *
     * @return
     */
    public static String digitToUppercase(String num) throws Exception {
        String fraction[] = {"角", "分"};
        String digit[] = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
        /**
         * 仟 佰 拾 ' ' ' ' $4 $3 $2 $1 万 $8 $7 $6 $5 亿 $12 $11 $10 $9
         */
        String unit1[] = {"", "拾", "佰", "仟"};// 把钱数分成段,每四个一段,实际上得到的是一个二维数组
        String unit2[] = {"元", "万", "亿", "万亿"}; // 把钱数分成段,每四个一段,实际上得到的是一个二维数组
        if (isEmpty(num)) {
            return "";
        }
        BigDecimal bigDecimal = new BigDecimal(num);
        bigDecimal = bigDecimal.multiply(new BigDecimal(100));
        String strVal = String.valueOf(bigDecimal.toBigInteger());
        String head = strVal.substring(0, strVal.length() - 2); // 整数部分
        String end = strVal.substring(strVal.length() - 2); // 小数部分
        String endMoney = "";
        String headMoney = "";
        if ("00".equals(end)) {
            endMoney = "整";
        } else {
            if (!end.substring(0, 1).equals("0")) {
                endMoney += digit[Integer.valueOf(end.substring(0, 1))] + "角";
            } else if (end.substring(0, 1).equals("0") && !end.substring(1, 2).equals("0")) {
                endMoney += "零";
            }
            if (!end.substring(1, 2).equals("0")) {
                endMoney += digit[Integer.valueOf(end.substring(1, 2))] + "分";
            }
        }
        char[] chars = head.toCharArray();
        Map<String, Boolean> map = new HashMap<String, Boolean>();// 段位置是否已出现zero
        boolean zeroKeepFlag = false;// 0连续出现标志
        int vidxtemp = 0;
        for (int i = 0; i < chars.length; i++) {
            int idx = (chars.length - 1 - i) % 4;// 段内位置 unit1
            int vidx = (chars.length - 1 - i) / 4;// 段位置 unit2
            String s = digit[Integer.valueOf(String.valueOf(chars[i]))];
            if (!"零".equals(s)) {
                headMoney += s + unit1[idx] + unit2[vidx];
                zeroKeepFlag = false;
            } else if (i == chars.length - 1 || map.get("zero" + vidx) != null) {
                headMoney += "";
            } else {
                headMoney += s;
                zeroKeepFlag = true;
                map.put("zero" + vidx, true);// 该段位已经出现0；
            }
            if (vidxtemp != vidx || i == chars.length - 1) {
                headMoney = headMoney.replaceAll(unit2[vidx], "");
                headMoney += unit2[vidx];
            }
            if (zeroKeepFlag && (chars.length - 1 - i) % 4 == 0) {
                headMoney = headMoney.replaceAll("零", "");
            }
        }
        return headMoney + endMoney;
    }

    /**
     * 将字符串中的中文转化为拼音,英文字符不变 (测试>ceshi)
     *
     * @param inputString 汉字
     * @return
     */
    public static String getPingYin(String inputString) {
        HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
        format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        format.setVCharType(HanyuPinyinVCharType.WITH_V);
        String output = "";
        if (inputString != null && inputString.length() > 0 && !"null".equals(inputString)) {
            char[] input = inputString.trim().toCharArray();
            try {
                for (int i = 0; i < input.length; i++) {
                    if (Character.toString(input[i]).matches("[\\u4E00-\\u9FA5]+")) {
                        String[] temp = PinyinHelper.toHanyuPinyinStringArray(input[i], format);
                        output += temp[0];
                    } else
                        output += Character.toString(input[i]);
                }
            } catch (BadHanyuPinyinOutputFormatCombination e) {
                e.printStackTrace();
            }
        } else {
            return "*";
        }
        return output;
    }

    /**
     * 获取uuid
     *
     * @return
     */
    public static String getUuid() {
        return UUID.randomUUID().toString().replace("-", "");
    }

    /**
     * -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
     * HTML过滤
     * -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
     */
    /**
     * 针对前台XXS
     *
     * @param str
     * @return
     */
    public static String htmlEncode(String str) {
        if (str == null || "".equals(str.trim()))
            return str;

        StringBuilder encodeStrBuilder = new StringBuilder();
        for (int i = 0, len = str.length(); i < len; i++) {
            encodeStrBuilder.append(htmlEncode1(str.charAt(i)));
        }
        return htmlWordsEncode(encodeStrBuilder.toString());
    }

    /**
     * 转换<>符号
     *
     * @param str
     * @return
     */
    public static String stringWash(String str) {
        if (str == null || "".equals(str.trim()))
            return str;

        StringBuilder encodeStrBuilder = new StringBuilder();
        for (int i = 0, len = str.length(); i < len; i++) {
            encodeStrBuilder.append(htmlEncode2(str.charAt(i)));
        }
        return htmlWordsEncode(encodeStrBuilder.toString());
    }

    /**
     * 逆转换<>符号
     *
     * @param str
     * @return
     */
    public static String stringWash2(String str) {
        if (null != str) {
            StringBuilder encodeStrBuilder = new StringBuilder();
            for (int i = 0, len = str.length(); i < len; i++) {
                encodeStrBuilder.append(htmlEncode3(str.charAt(i)));
            }
            return encodeStrBuilder.toString();
        } else {
            return str;
        }
    }

    /**
     * 特殊HTML字符转换
     *
     * @param c
     * @return
     */
    public static String htmlEncode(char c) {
        switch (c) {
            case '&':
                return "&amp;";
            case '<':
                return "&lt;";
            case '>':
                return "&gt;";
            case '"':
                return "&quot;";
            case ' ':
                return "&nbsp;";
            default:
                return c + "";
        }
    }

    /**
     * 特殊HTML字符转换(半角转全角)
     *
     * @param c
     * @return
     */
    public static String htmlEncode1(char c) {
        switch (c) {
            case '&':
                return "＆";
            case '<':
                return "＜";
            case '>':
                return "＞";
            case '"':
                return "＂";
            case '(':
                return "（";
            case ')':
                return "）";
            case ';':
                return " ；";
            default:
                return c + "";
        }
    }

    public static String htmlEncode2(char c) {
        switch (c) {
            case '<':
                return "＜";
            case '>':
                return "＞";
            default:
                return c + "";
        }
    }

    public static String htmlEncode3(char c) {
        switch (c) {
            case '＜':
                return "<";
            case '＞':
                return ">";
            default:
                return c + "";
        }
    }

    // 标点全角半角切换
    public static String htmlWordsExchange(String str) {
        if (str.indexOf(",") > -1) {
            str = str.replaceAll(",", "，");
        }

        if (str.indexOf("\"") > -1) {
            str = str.replaceAll("\"", "＂");
        }

        return str;
    }

    //html类型字段过滤
    public static String htmlFieldWordsEncode(String value) {
        if (value != null) {
            Pattern scriptPattern = Pattern.compile("<script(.*?)>",
                    Pattern.CASE_INSENSITIVE | Pattern.MULTILINE
                            | Pattern.DOTALL);

            if (scriptPattern.matcher(value).find()) {
                value = value.replace("<script", "＜ｓｃｒｉｐｔ");
            }
            scriptPattern = Pattern.compile("</script(.*?)>",
                    Pattern.CASE_INSENSITIVE);

            if (scriptPattern.matcher(value).find()) {
                value = value.replace("</script", "＜／ｓｃｒｉｐｔ");
            }

            scriptPattern = Pattern.compile("onload(.*?)=",
                    Pattern.CASE_INSENSITIVE | Pattern.MULTILINE
                            | Pattern.DOTALL);

            if (scriptPattern.matcher(value).find()) {
                value = value.replace("onload", "ｏｎｌｏａｄ");
            }

            scriptPattern = Pattern.compile("(.*?).innerHTML",
                    Pattern.CASE_INSENSITIVE | Pattern.MULTILINE
                            | Pattern.DOTALL);

            if (scriptPattern.matcher(value).find()) {
                value = value.replace("innerHTML", "ｉｎｎｅｒＨＴＭＬ");
            }

            scriptPattern = Pattern.compile("alert\\((.*?)\\)",
                    Pattern.CASE_INSENSITIVE | Pattern.MULTILINE
                            | Pattern.DOTALL);
            if (scriptPattern.matcher(value).find()) {
                value = value.replace("alert", "ａｌｅｒｔ");
            }
            scriptPattern = Pattern.compile("eval\\((.*?)\\)",
                    Pattern.CASE_INSENSITIVE | Pattern.MULTILINE
                            | Pattern.DOTALL);
            if (scriptPattern.matcher(value).find()) {
                value = value.replace("eval", "ｅｖａｌ");
            }
            scriptPattern = Pattern.compile("console.log\\((.*?)\\)",
                    Pattern.CASE_INSENSITIVE | Pattern.MULTILINE
                            | Pattern.DOTALL);
            if (scriptPattern.matcher(value).find()) {
                value = value.replace("console.log", "console.log");
            }
            scriptPattern = Pattern.compile("confirm\\((.*?)\\)",
                    Pattern.CASE_INSENSITIVE | Pattern.MULTILINE
                            | Pattern.DOTALL);
            if (scriptPattern.matcher(value).find()) {
                value = value.replace("confirm", "ｃｏｎｆｉｒｍ");
            }
            scriptPattern = Pattern.compile("prompt\\((.*?)\\)",
                    Pattern.CASE_INSENSITIVE | Pattern.MULTILINE
                            | Pattern.DOTALL);
            if (scriptPattern.matcher(value).find()) {
                value = value.replace("prompt", "ｐｒｏｍｐｔ");
            }


            scriptPattern = Pattern.compile("onclick(.*?)=",
                    Pattern.CASE_INSENSITIVE | Pattern.MULTILINE
                            | Pattern.DOTALL);

            if (scriptPattern.matcher(value).find()) {
                value = value.replace("onclick", "ｏｎｃｌｉｃｋ");
            }
            scriptPattern = Pattern.compile("ondblclick(.*?)=",
                    Pattern.CASE_INSENSITIVE | Pattern.MULTILINE
                            | Pattern.DOTALL);

            if (scriptPattern.matcher(value).find()) {
                value = value.replace("ondblclick", "ｏｎｄｂｌｃｌｉｃｋ");
            }


            scriptPattern = Pattern.compile("onmouseover(.*?)=",
                    Pattern.CASE_INSENSITIVE | Pattern.MULTILINE
                            | Pattern.DOTALL);

            if (scriptPattern.matcher(value).find()) {
                value = value.replace("onmouseover", "ｏｎｍｏｕｓｅｏｖｅｒ");
            }
            scriptPattern = Pattern.compile("onmouseout(.*?)=",
                    Pattern.CASE_INSENSITIVE | Pattern.MULTILINE
                            | Pattern.DOTALL);

            if (scriptPattern.matcher(value).find()) {
                value = value.replace("onmouseout", "ｏｎｍｏｕｓｅｏｕｔ");
            }


            scriptPattern = Pattern.compile("window.(.*?)",
                    Pattern.CASE_INSENSITIVE | Pattern.MULTILINE
                            | Pattern.DOTALL);

            if (scriptPattern.matcher(value).find()) {
                value = value.replace("window", "ｗｉｎｄｏｗ");
            }

            scriptPattern = Pattern.compile("document.(.*?)",
                    Pattern.CASE_INSENSITIVE | Pattern.MULTILINE
                            | Pattern.DOTALL);

            if (scriptPattern.matcher(value).find()) {
                value = value.replace("document", "ｄｏｃｕｍｎｎｔ");
            }

        }
        return value;
    }

    /**
     * Xss js 脚本过滤
     *
     * @param parameterValue
     * @return
     */
    public static String xssScript(String parameterValue) {
        if (StringUtils.isNotBlank(parameterValue)) {
            // 移除<script></script>
            Pattern scriptPattern = Pattern.compile("<script(.*?)>(.*?)</script>", Pattern.CASE_INSENSITIVE);
            Matcher matcher = scriptPattern.matcher(parameterValue);
            while (matcher.find()) {
                String group = matcher.group();
                String newGroup = group.replaceAll("(?i)script", "ｓｃｒｉｐｔ");
                parameterValue = parameterValue.replace(group, newGroup);
            }

            // 删除独立的</script>标签
            scriptPattern = Pattern.compile("</script>", Pattern.CASE_INSENSITIVE);
            matcher = scriptPattern.matcher(parameterValue);
            while (matcher.find()) {
                String group = matcher.group();
                String newGroup = group.replaceAll("(?i)script", "ｓｃｒｉｐｔ");
                parameterValue = parameterValue.replace(group, newGroup);
            }

            // 删除独立的<script >标签
            scriptPattern = Pattern.compile("<script(.*?)>", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
            matcher = scriptPattern.matcher(parameterValue);
            while (matcher.find()) {
                String group = matcher.group();
                String newGroup = group.replaceAll("(?i)script", "ｓｃｒｉｐｔ");
                parameterValue = parameterValue.replace(group, newGroup);
            }

            // 排除使用 src='...' 类型的表达式
            scriptPattern = Pattern.compile("src[\\s]*=[\\s]*\\\'(.*?)\\\'", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
            matcher = scriptPattern.matcher(parameterValue);
            while (matcher.find()) {
                String group = matcher.group();
                String newGroup = group.replaceAll("(?i)src", "ｓｒｃ");
                parameterValue = parameterValue.replace(group, newGroup);
            }

            // 排除使用 src="..." 类型的表达式
            scriptPattern = Pattern.compile("src[\\s]*=[\\s]*\\\"(.*?)\\\"", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
            matcher = scriptPattern.matcher(parameterValue);
            while (matcher.find()) {
                String group = matcher.group();
                String newGroup = group.replaceAll("(?i)src", "ｓｒｃ");
                parameterValue = parameterValue.replace(group, newGroup);
            }

            // 排除使用 href='...' 类型的表达式
            scriptPattern = Pattern.compile("href[\\s]*=[\\s]*\\\'(.*?)\\\'", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
            matcher = scriptPattern.matcher(parameterValue);
            while (matcher.find()) {
                String group = matcher.group();
                String newGroup = group.replaceAll("(?i)href", "ｈｒｅｆ");
                parameterValue = parameterValue.replace(group, newGroup);
            }

            // 排除使用 href="..."'" 类型的表达式
            scriptPattern = Pattern.compile("href[\\s]*=[\\s]*\\\"(.*?)\\\"", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
            matcher = scriptPattern.matcher(parameterValue);
            while (matcher.find()) {
                String group = matcher.group();
                String newGroup = group.replaceAll("(?i)href", "ｈｒｅｆ");
                parameterValue = parameterValue.replace(group, newGroup);
            }

            // 排除使用 alert(...) 表达式
            scriptPattern = Pattern.compile("alert\\((.*?)\\)", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
            matcher = scriptPattern.matcher(parameterValue);
            while (matcher.find()) {
                String group = matcher.group();
                String newGroup = group.replaceAll("(?i)alert", "ａｌｅｒｔ");
                parameterValue = parameterValue.replace(group, newGroup);
            }

            // 排除使用 confirm(...) 表达式
            scriptPattern = Pattern.compile("confirm\\((.*?)\\)", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
            matcher = scriptPattern.matcher(parameterValue);
            while (matcher.find()) {
                String group = matcher.group();
                String newGroup = group.replaceAll("(?i)confirm", "ｃｏｎｆｉｒｍ");
                parameterValue = parameterValue.replace(group, newGroup);
            }

            // 排除使用 prompt(...) 表达式
            scriptPattern = Pattern.compile("prompt\\((.*?)\\)", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
            matcher = scriptPattern.matcher(parameterValue);
            while (matcher.find()) {
                String group = matcher.group();
                String newGroup = group.replaceAll("(?i)prompt", "ｐｒｏｍｐｔ");
                parameterValue = parameterValue.replace(group, newGroup);
            }

            // 排除使用 eval(...) 表达式
            scriptPattern = Pattern.compile("eval\\((.*?)\\)", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
            matcher = scriptPattern.matcher(parameterValue);
            while (matcher.find()) {
                String group = matcher.group();
                String newGroup = group.replaceAll("(?i)eval", "ｅｖａｌ");
                parameterValue = parameterValue.replace(group, newGroup);
            }

            // 排除使用 expression(...) 表达式
            scriptPattern = Pattern.compile("expression\\((.*?)\\)", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
            matcher = scriptPattern.matcher(parameterValue);
            while (matcher.find()) {
                String group = matcher.group();
                String newGroup = group.replaceAll("(?i)expression", "ｅｘｐｒｅｓｓｉｏｎ");
                parameterValue = parameterValue.replace(group, newGroup);
            }

            // 排除使用 javascript:... 表达式
            scriptPattern = Pattern.compile("javascript:", Pattern.CASE_INSENSITIVE);
            matcher = scriptPattern.matcher(parameterValue);
            while (matcher.find()) {
                String group = matcher.group();
                String newGroup = group.replaceAll("(?i)javascript", "ｊａｖａｓｃｒｉｐｔ");
                parameterValue = parameterValue.replaceAll(group, newGroup);
            }

            // 排除使用 window. 表达式
            scriptPattern = Pattern.compile("window\\.", Pattern.CASE_INSENSITIVE);
            matcher = scriptPattern.matcher(parameterValue);
            while (matcher.find()) {
                String group = matcher.group();
                String newGroup = group.replaceAll("(?i)window", "ｗｉｎｄｏｗ");
                parameterValue = parameterValue.replaceAll(group, newGroup);
            }

            // 排除使用 vbscript:... 表达式
            scriptPattern = Pattern.compile("vbscript:", Pattern.CASE_INSENSITIVE);
            matcher = scriptPattern.matcher(parameterValue);
            while (matcher.find()) {
                String group = matcher.group();
                String newGroup = group.replaceAll("(?i)vbscript", "ｖｂｓｃｒｉｐｔ");
                parameterValue = parameterValue.replaceAll(group, newGroup);
            }

            // 排除使用 onload=... 表达式
            scriptPattern = Pattern.compile("onload(.*?)=", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
            matcher = scriptPattern.matcher(parameterValue);
            while (matcher.find()) {
                String group = matcher.group();
                String newGroup = group.replaceAll("(?i)onload", "ｏｎｌｏａｄ");
                parameterValue = parameterValue.replace(group, newGroup);
            }

            // 排除使用 onclick=... 表达式
            scriptPattern = Pattern.compile("onclick(.*?)=", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
            matcher = scriptPattern.matcher(parameterValue);
            while (matcher.find()) {
                String group = matcher.group();
                String newGroup = group.replaceAll("(?i)onclick", "ｏｎｃｌｉｃｋ");
                parameterValue = parameterValue.replace(group, newGroup);
            }

            // 移除<iframe></iframe>
            scriptPattern = Pattern.compile("<iframe>(.*?)</iframe>", Pattern.CASE_INSENSITIVE);
            matcher = scriptPattern.matcher(parameterValue);
            while (matcher.find()) {
                String group = matcher.group();
                String newGroup = group.replaceAll("(?i)iframe", "ｉｆｒａｍｅ");
                parameterValue = parameterValue.replace(group, newGroup);
            }

            // 删除独立的</iframe>标签
            scriptPattern = Pattern.compile("</iframe>", Pattern.CASE_INSENSITIVE);
            matcher = scriptPattern.matcher(parameterValue);
            while (matcher.find()) {
                String group = matcher.group();
                String newGroup = group.replaceAll("(?i)iframe", "ｉｆｒａｍｅ");
                parameterValue = parameterValue.replace(group, newGroup);
            }

            // 删除独立的<iframe>标签
            scriptPattern = Pattern.compile("<iframe(.*?)>", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
            matcher = scriptPattern.matcher(parameterValue);
            while (matcher.find()) {
                String group = matcher.group();
                String newGroup = group.replaceAll("(?i)iframe", "ｉｆｒａｍｅ");
                parameterValue = parameterValue.replace(group, newGroup);
            }
        }
        return parameterValue;
    }

    /**
     * 验证是否存在xss脚本
     * @param parameterValue
     * @return
     */
    public static boolean exitsXxScript(String parameterValue) {
        if (StringUtils.isNotBlank(parameterValue)) {
            // 是否存在<script></script>
            Pattern scriptPattern = Pattern.compile("<script(.*?)>(.*?)</script>", Pattern.CASE_INSENSITIVE);
            Matcher matcher = scriptPattern.matcher(parameterValue);
            if (matcher.find()) {
                return true;
            }

            // 是否存在独立的</script>标签
            scriptPattern = Pattern.compile("</script>", Pattern.CASE_INSENSITIVE);
            matcher = scriptPattern.matcher(parameterValue);
            if (matcher.find()) {
                return true;
            }

            // 是否存在独立的<script >标签
            scriptPattern = Pattern.compile("<script(.*?)>", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
            matcher = scriptPattern.matcher(parameterValue);
            if (matcher.find()) {
                return true;
            }

            // 是否使用 src='...' 类型的表达式
            scriptPattern = Pattern.compile("src[\\s]*=[\\s]*\\\'(.*?)\\\'", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
            matcher = scriptPattern.matcher(parameterValue);
            if (matcher.find()) {
                return true;
            }

            // 是否使用 src="..." 类型的表达式
            scriptPattern = Pattern.compile("src[\\s]*=[\\s]*\\\"(.*?)\\\"", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
            matcher = scriptPattern.matcher(parameterValue);
            if (matcher.find()) {
                return true;
            }

            // 是否使用 href='...' 类型的表达式
            scriptPattern = Pattern.compile("href[\\s]*=[\\s]*\\\'(.*?)\\\'", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
            matcher = scriptPattern.matcher(parameterValue);
            if (matcher.find()) {
                return true;
            }

            // 是否使用 href="..."'" 类型的表达式
            scriptPattern = Pattern.compile("href[\\s]*=[\\s]*\\\"(.*?)\\\"", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
            matcher = scriptPattern.matcher(parameterValue);
            if (matcher.find()) {
                return true;
            }

            // 是否使用 alert(...) 表达式
            scriptPattern = Pattern.compile("alert\\((.*?)\\)", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
            matcher = scriptPattern.matcher(parameterValue);
            if (matcher.find()) {
                return true;
            }

            // 是否使用 confirm(...) 表达式
            scriptPattern = Pattern.compile("confirm\\((.*?)\\)", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
            matcher = scriptPattern.matcher(parameterValue);
            if (matcher.find()) {
                return true;
            }

            // 是否使用 prompt(...) 表达式
            scriptPattern = Pattern.compile("prompt\\((.*?)\\)", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
            matcher = scriptPattern.matcher(parameterValue);
            if (matcher.find()) {
                return true;
            }

            // 是否使用 eval(...) 表达式
            scriptPattern = Pattern.compile("eval\\((.*?)\\)", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
            matcher = scriptPattern.matcher(parameterValue);
            if (matcher.find()) {
                return true;
            }

            // 是否使用 expression(...) 表达式
            scriptPattern = Pattern.compile("expression\\((.*?)\\)", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
            matcher = scriptPattern.matcher(parameterValue);
            if (matcher.find()) {
                return true;
            }

            // 是否使用 javascript:... 表达式
            scriptPattern = Pattern.compile("javascript:", Pattern.CASE_INSENSITIVE);
            matcher = scriptPattern.matcher(parameterValue);
            if (matcher.find()) {
                return true;
            }

            // 是否使用 window. 表达式
            scriptPattern = Pattern.compile("window\\.", Pattern.CASE_INSENSITIVE);
            matcher = scriptPattern.matcher(parameterValue);
            if (matcher.find()) {
                return true;
            }

            // 是否使用 vbscript:... 表达式
            scriptPattern = Pattern.compile("vbscript:", Pattern.CASE_INSENSITIVE);
            matcher = scriptPattern.matcher(parameterValue);
            if (matcher.find()) {
                return true;
            }

            // 是否使用 onload=... 表达式
            scriptPattern = Pattern.compile("onload(.*?)=", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
            matcher = scriptPattern.matcher(parameterValue);
            if (matcher.find()) {
                return true;
            }

            // 是否使用 onclick=... 表达式
            scriptPattern = Pattern.compile("onclick(.*?)=", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
            matcher = scriptPattern.matcher(parameterValue);
            if (matcher.find()) {
                return true;
            }

            // 是否存在<iframe></iframe>
            scriptPattern = Pattern.compile("<iframe>(.*?)</iframe>", Pattern.CASE_INSENSITIVE);
            matcher = scriptPattern.matcher(parameterValue);
            if (matcher.find()) {
                return true;
            }

            // 是否存在独立的</iframe>标签
            scriptPattern = Pattern.compile("</iframe>", Pattern.CASE_INSENSITIVE);
            matcher = scriptPattern.matcher(parameterValue);
            if (matcher.find()) {
                return true;
            }

            // 是否存在独立的<iframe>标签
            scriptPattern = Pattern.compile("<iframe(.*?)>", Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
            matcher = scriptPattern.matcher(parameterValue);
            if (matcher.find()) {
                return true;
            }
        }
        return false;
    }

    public static String htmlWordsEncode(String str) {
        if (str.indexOf("script") > -1) {
            str = str.replaceAll("script", "ｓｃｒｉｐｔ");
        }

        if (str.indexOf("document") > -1) {
            str = str.replaceAll("document", "ｄｏｃｕｍｅｎｔ");
        }

        if (str.indexOf("eval") > -1) {
            str = str.replaceAll("eval", "ｅｖａｌ");
        }

        if (str.indexOf("alert") > -1) {
            str = str.replaceAll("alert", "ａｌｅｒｔ");
        }

        if (str.indexOf("confirm") > -1) {
            str = str.replaceAll("confirm", "ｃｏｎ fｉｒｍ");
        }

        if (str.indexOf("prompt") > -1) {
            str = str.replaceAll("prompt", "ｐｒｏｍｐｔ");
        }

        if (str.indexOf("onmouseover") > -1) {
            str = str.replaceAll("onmouseover", "ｏｎｍｏｕｓｅｏｖｅｒ");
        }

        if (str.indexOf("window") > -1) {
            str = str.replaceAll("window", "ｗｉｎｄｏｗ");
        }

        if (str.indexOf("onclick") > -1) {
            str = str.replaceAll("onclick", "ｏｎｃｌｉｃｋ");
        }
        return str;
    }

    /**
     * 检验是否为弱密码 字母、数字、符号(8-16位)
     *
     * @param password
     */
    public static boolean checkWeakPassword(String password) {
        String regex = "^(?:(?=.*[0-9].*)(?=.*[A-Za-z].*)(?=.*[\\\"~'`!,.;|@#￥$%^?&*({\\[\\]<>})--+_=:].*))[\\W0-9A-Za-z]{8,16}$";
        return password.matches(regex);
    }

    /**
     * 从字符串中得到EL表达式内容
     *
     * @param str
     * @return
     */
    public static List<String> getElParam(String str) {
        List<String> list = new ArrayList<String>();
        try {
            String regex = "(#\\{)[^#]+(\\})";
            Pattern p = Pattern.compile(regex);
            Matcher m = p.matcher(str);
            while (m.find()) {
                list.add(m.group(0));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * <p>
     * 将提供的<code> Collection </ code>的元素连接到包含所提供元素的单个String中。</ p>
     * <p>
     * 在列表之前或之后不添加分隔符。 <code> null </ code>分隔符与空String（""）相同。</ p>
     * <p>
     * 请参阅此处的示例：{@ link #join（Object []，String）}。</ p>
     * <p>
     * &#64;param collection 要连接在一起的值的<code> Collection </ code>可能为null
     * &#64;param separator 要使用的分隔符，null视为""
     *
     * @return 如果<code> Collection </ code>的或其迭代器为null，返回null，否则加入String<code>
     * null </ code>
     */
    public static String join(Collection collection, String separator) {
        return StringUtils.join(collection, separator);
    }

    public static boolean isJsonArray(String str) {
        try {
            JSONArray jsonStr = JSONArray.parseArray(str);
            return true;
        } catch (Exception e) {
            return false;
        }
    }


    public static boolean validateBrace(String str, char a) {
        boolean flag = true;
        if (StringUtils.isNotBlank(str)) {
            str = str.replaceAll(" ", "");
            for (int i = 0, len = str.length(); i < len; i++) {
                if (str.charAt(i) != a) {
                    flag = false;
                    break;
                }
            }
        }
        return flag;
    }


    public static String escapeCharacter(String str) {
        if (StringUtil.notNull(str)) {
            str = str.replace("<", "＜").
                    replace(">", "＞").replace("(", "（").replace(")", "）");
        }
        return str;
    }

}
